import { isArray } from "lodash";
import {
  fetch_comments_by_case_id,
} from "../api/api.js";

const state = {
  comments: []
};

const getters = {
  // 顶级消息
  top_comments: (state, getters, rootState) => {
    return state.comments.filter(r => !r.comment_id);
  },

  // 回复消息
  replies: (state, getters, rootState) => {
    return state.comments.filter(r => r.comment_id)
  }
};

// actions处理异步
const actions = {
  // 获取案件的评论列表
  fetch_comments({ commit }, { payload }) {
    fetch_comments_by_case_id(payload).then((res) => {
      commit("setComments", res);
    });
  },
};

// mutation 必须同步，任何异步只能出现在 action 里
const mutations = {
  setComments(state, payload) {
    state.comments = payload;
  },
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};
